home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Programming / AmigaE / Src / Rkrm / Graphics_Libraries / Layers / layers.e < prev   
Text File  |  1995-09-20  |  11KB  |  372 lines

  1. -> Layers.e
  2.  
  3. ->>> Header (globals)
  4. OPT PREPROCESS
  5.  
  6. MODULE 'layers',
  7.        'graphics/clip',
  8.        'graphics/layers',
  9.        'graphics/gfx',
  10.        'graphics/gfxbase',
  11.        'graphics/rastport',
  12.        'graphics/view'
  13.  
  14. ENUM ERR_NONE, ERR_BLYR, ERR_CMAP, ERR_LIB, ERR_LYR
  15.  
  16. RAISE ERR_BLYR IF CreateBehindLayer()=NIL,
  17.       ERR_CMAP IF GetColorMap()=NIL,
  18.       ERR_LIB  IF OpenLibrary()=NIL,
  19.       ERR_LYR  IF NewLayerInfo()=NIL
  20.  
  21. CONST L_DELAY=100, S_DELAY=50, DUMMY=0
  22.  
  23. ENUM RED_PEN=1, GREEN_PEN, BLUE_PEN
  24.  
  25. CONST SCREEN_D=2, SCREEN_W=320, SCREEN_H=200
  26.  
  27. -> The starting size of example layers, offsets are used for placement.
  28. CONST W_H=50, W_T=5, W_W=80
  29. CONST W_B=W_T+W_H-1
  30. CONST SWH=SCREEN_W/2, WWH=W_W/2
  31. CONST W_L=SWH-WWH
  32. CONST W_R=W_L+W_W-1
  33.  
  34. -> Size of the superbitmap.
  35. CONST SUPER_H=SCREEN_H, SUPER_W=SCREEN_W
  36.  
  37. -> Starting size of the message layer.
  38. CONST M_H=10, M_W=SCREEN_W, M_L=0
  39. CONST M_T=SCREEN_H-M_H
  40. CONST M_B=M_T+M_H-1
  41. CONST M_R=M_L+M_W-1
  42.  
  43. DEF theLayerFlags:PTR TO LONG, colourtable:PTR TO INT
  44. ->>>
  45.  
  46. ->>> PROC myLabelLayer(layer:PTR TO layer, colour, string)
  47. -> Clear the layer THEN draw in a text string.
  48. PROC myLabelLayer(layer:PTR TO layer, colour, string)
  49.   -> Fill layer with colour
  50.   SetRast(layer.rp, colour)
  51.  
  52.   -> Set up for writing text into layer
  53.   SetDrMd(layer.rp, RP_JAM1)
  54.   SetAPen(layer.rp, 0)
  55.   Move(layer.rp, 5, 7)
  56.  
  57.   -> Write into layer
  58.   Text(layer.rp, string, StrLen(string))
  59. ENDPROC
  60. ->>>
  61.  
  62. ->>> PROC pMessage(layer, string)
  63. -> Write a message into a layer with a delay.
  64. PROC pMessage(layer, string)
  65.   Delay(S_DELAY)
  66.   myLabelLayer(layer, GREEN_PEN, string)
  67. ENDPROC
  68. ->>>
  69.  
  70. ->>> PROC error(layer, string)
  71. -> Write an error message into a layer with a delay.
  72. PROC error(layer, string)
  73.   myLabelLayer(layer, RED_PEN, string)
  74.   Delay(L_DELAY)
  75. ENDPROC
  76. ->>>
  77.  
  78. ->>> PROC doLayers(msgLayer, layer_array:PTR TO LONG)
  79. -> Do some layers manipulations to demonstrate their abilities.
  80. PROC doLayers(msgLayer, layer_array:PTR TO LONG)
  81.   DEF ktr, ktr_2, tlayer:PTR TO layer
  82.  
  83.   pMessage(msgLayer, 'Label all Layers')
  84.   myLabelLayer(layer_array[0], RED_PEN,   'SUPER')
  85.   myLabelLayer(layer_array[1], GREEN_PEN, 'Smart')
  86.   myLabelLayer(layer_array[2], BLUE_PEN,  'Simple')
  87.  
  88.   pMessage(msgLayer, 'MoveLayer 1 InFrontOf 0')
  89.   IF FALSE=MoveLayerInFrontOf(layer_array[1], layer_array[0])
  90.     error(msgLayer, 'MoveLayerInFrontOf() failed.')
  91.   ENDIF
  92.  
  93.   pMessage(msgLayer, 'MoveLayer 2 InFrontOf 1')
  94.   IF FALSE=MoveLayerInFrontOf(layer_array[2], layer_array[1])
  95.     error(msgLayer, 'MoveLayerInFrontOf() failed.')
  96.   ENDIF
  97.  
  98.   pMessage(msgLayer, 'Refresh Simple Refresh Layer')
  99.   myLabelLayer(layer_array[2], BLUE_PEN, 'Simple')
  100.  
  101.   pMessage(msgLayer, 'Incrementally MoveLayers...')
  102.   FOR ktr:=0 TO 29
  103.     IF FALSE=MoveLayer(DUMMY, layer_array[1], -1, 0)
  104.       error(msgLayer, 'MoveLayer() failed.')
  105.     ENDIF
  106.     IF FALSE=MoveLayer(DUMMY, layer_array[2], -2, 0)
  107.       error(msgLayer, 'MoveLayer() failed.')
  108.     ENDIF
  109.   ENDFOR
  110.  
  111.   pMessage(msgLayer, 'Refresh Simple Refresh Layer')
  112.   myLabelLayer(layer_array[2], BLUE_PEN, 'Simple')
  113.  
  114.   pMessage(msgLayer, 'Make Layer 0 the UpfrontLayer')
  115.   IF FALSE=UpfrontLayer(DUMMY, layer_array[0])
  116.     error(msgLayer, 'UpfrontLayer() failed.')
  117.   ENDIF
  118.  
  119.   pMessage(msgLayer, 'Make Layer 2 the BehindLayer')
  120.   IF FALSE=BehindLayer(DUMMY, layer_array[2])
  121.     error(msgLayer, 'BehindLayer() failed.')
  122.   ENDIF
  123.  
  124.   pMessage(msgLayer, 'Incrementally MoveLayers again...')
  125.   FOR ktr:=0 TO 29
  126.     IF FALSE=MoveLayer(DUMMY, layer_array[1], 0, 1)
  127.       error(msgLayer, 'MoveLayer() failed.')
  128.     ENDIF
  129.     IF FALSE=MoveLayer(DUMMY, layer_array[2], 0, 2)
  130.       error(msgLayer, 'MoveLayer() failed.')
  131.     ENDIF
  132.   ENDFOR
  133.  
  134.   pMessage(msgLayer, 'Refresh Simple Refresh Layer')
  135.   myLabelLayer(layer_array[2], BLUE_PEN, 'Simple')
  136.  
  137.   pMessage(msgLayer, 'Big MoveLayer')
  138.   FOR ktr:=0 TO 2
  139.     tlayer:=layer_array[ktr]
  140.     IF FALSE=MoveLayer(DUMMY, tlayer, -tlayer.minx, 0)
  141.       error(msgLayer, 'MoveLayer() failed.')
  142.     ENDIF
  143.   ENDFOR
  144.  
  145.   pMessage(msgLayer, 'Incrementally increase size')
  146.   FOR ktr:=0 TO 4
  147.     FOR ktr_2:=0 TO 2
  148.       IF FALSE=SizeLayer(DUMMY, layer_array[ktr_2], 1, 1)
  149.         error(msgLayer, 'SizeLayer() failed.')
  150.       ENDIF
  151.     ENDFOR
  152.   ENDFOR
  153.  
  154.   pMessage(msgLayer, 'Refresh Smart Refresh Layer')
  155.   myLabelLayer(layer_array[1], GREEN_PEN, 'Smart')
  156.   pMessage(msgLayer, 'Refresh Simple Refresh Layer')
  157.   myLabelLayer(layer_array[2], BLUE_PEN,  'Simple')
  158.  
  159.   pMessage(msgLayer, 'Big SizeLayer')
  160.   FOR ktr:=0 TO 2
  161.     tlayer:=layer_array[ktr]
  162.     IF FALSE=SizeLayer(DUMMY, tlayer, SCREEN_W-tlayer.maxx-1, 0)
  163.       error(msgLayer, 'SizeLayer() failed.')
  164.     ENDIF
  165.   ENDFOR
  166.  
  167.   pMessage(msgLayer, 'Refresh Smart Refresh Layer')
  168.   myLabelLayer(layer_array[1], GREEN_PEN, 'Smart')
  169.   pMessage(msgLayer, 'Refresh Simple Refresh Layer')
  170.   myLabelLayer(layer_array[2], BLUE_PEN,  'Simple')
  171.  
  172.   pMessage(msgLayer, 'ScrollLayer down')
  173.   FOR ktr:=0 TO 29
  174.     FOR ktr_2:=0 TO 2
  175.       ScrollLayer(DUMMY, layer_array[ktr_2], 0, -1)
  176.     ENDFOR
  177.   ENDFOR
  178.  
  179.   pMessage(msgLayer, 'Refresh Smart Refresh Layer')
  180.   myLabelLayer(layer_array[1], GREEN_PEN, 'Smart')
  181.   pMessage(msgLayer, 'Refresh Simple Refresh Layer')
  182.   myLabelLayer(layer_array[2], BLUE_PEN,  'Simple')
  183.  
  184.   pMessage(msgLayer, 'ScrollLayer up')
  185.   FOR ktr:=0 TO 29
  186.     FOR ktr_2:=0 TO 2
  187.       ScrollLayer(DUMMY, layer_array[ktr_2], 0, 1)
  188.     ENDFOR
  189.   ENDFOR
  190.  
  191.   pMessage(msgLayer, 'Refresh Smart Refresh Layer')
  192.   myLabelLayer(layer_array[1], GREEN_PEN, 'Smart')
  193.   pMessage(msgLayer, 'Refresh Simple Refresh Layer')
  194.   myLabelLayer(layer_array[2], BLUE_PEN,  'Simple')
  195.  
  196.   Delay(L_DELAY)
  197. ENDPROC
  198. ->>>
  199.  
  200. ->>> PROC disposeLayers(msgLayer, layer_array:PTR TO LONG)
  201. -> Delete the layer array created by allocLayers().
  202. PROC disposeLayers(msgLayer, layer_array:PTR TO LONG)
  203.   DEF ktr
  204.   FOR ktr:=0 TO 2
  205.     IF layer_array[ktr]
  206.       IF FALSE=DeleteLayer(DUMMY, layer_array[ktr])
  207.         error(msgLayer, 'Error deleting layer')
  208.       ENDIF
  209.     ENDIF
  210.   ENDFOR
  211. ENDPROC
  212. ->>>
  213.  
  214. ->>> PROC allocLayers(msgLayer,layer_array,super_bitmap,theLayerInfo,theBitMap)
  215. -> Create some hard-coded layers.  The first must be SUPER-bitmap, with the
  216. -> bitmap passed as an argument.  The others must not be super-bitmap. The
  217. -> pointers to the created layers are returned in layer_array.
  218. ->
  219. -> Return FALSE on failure.  On a FALSE return, the layers are properly
  220. -> cleaned up.
  221. PROC allocLayers(msgLayer, layer_array:PTR TO LONG, super_bitmap,
  222.                  theLayerInfo, theBitMap) HANDLE
  223.   DEF ktr, tlayer:PTR TO layer
  224.   FOR ktr:=0 TO 2
  225.     pMessage(msgLayer, 'Create BehindLayer')
  226.     IF ktr=0
  227.       tlayer:=CreateBehindLayer(theLayerInfo, theBitMap,
  228.                   W_L+(ktr*30), W_T+(ktr*30), W_R+(ktr*30), W_B+(ktr*30),
  229.                   theLayerFlags[ktr], super_bitmap)
  230.     ELSE
  231.       tlayer:=CreateBehindLayer(theLayerInfo, theBitMap,
  232.                   W_L+(ktr*30), W_T+(ktr*30), W_R+(ktr*30), W_B+(ktr*30),
  233.                   theLayerFlags[ktr], NIL)
  234.     ENDIF
  235.  
  236.     pMessage(msgLayer, 'Fill the RastPort')
  237.     SetRast(tlayer.rp, ktr+1)
  238.     layer_array[ktr]:=tlayer
  239.   ENDFOR
  240. EXCEPT
  241.   disposeLayers(msgLayer, layer_array)
  242.   ReThrow()
  243. ENDPROC
  244. ->>>
  245.  
  246. ->>> PROC disposeBitMap(bitmap:PTR TO bitmap, depth, width, height)
  247. -> Free the bitmap and all bitplanes created by allocBitMap().
  248. PROC disposeBitMap(bitmap:PTR TO bitmap, depth, width, height)
  249.   DEF ktr
  250.   IF bitmap
  251.     FOR ktr:=0 TO depth-1
  252.       IF bitmap.planes[ktr] THEN FreeRaster(bitmap.planes[ktr], width, height)
  253.     ENDFOR
  254.     Dispose(bitmap)
  255.   ENDIF
  256. ENDPROC
  257. ->>>
  258.  
  259. ->>> PROC allocBitMap(depth, width, height)
  260. -> Allocate and initialize a bitmap structure.
  261. PROC allocBitMap(depth, width, height) HANDLE
  262.   DEF ktr, bitmap=NIL:PTR TO bitmap
  263.   NEW bitmap
  264.   InitBitMap(bitmap, depth, width, height)
  265.  
  266.   FOR ktr:=0 TO depth-1
  267.     bitmap.planes[ktr]:=AllocRaster(width, height)
  268.     BltClear(bitmap.planes[ktr], RASSIZE(width, height), 1)
  269.   ENDFOR
  270. EXCEPT
  271.   disposeBitMap(bitmap, depth, width, height)
  272.   ReThrow()
  273. ENDPROC bitmap
  274. ->>>
  275.  
  276. ->>> PROC startLayers(theLayerInfo, theBitMap)
  277. -> Set up to run the layers example, doLayers(). Clean up when done.
  278. PROC startLayers(theLayerInfo, theBitMap) HANDLE
  279.   DEF msgLayer=NIL, theSuperBitMap=NIL, theLayers=NIL
  280.   theLayers:=[NIL, NIL, NIL]:LONG
  281.   IF msgLayer:=CreateUpfrontLayer(theLayerInfo, theBitMap,
  282.                                   M_L, M_T, M_R, M_B, LAYERSMART, NIL)
  283.     pMessage(msgLayer, 'Setting up Layers')
  284.     theSuperBitMap:=allocBitMap(SCREEN_D, SUPER_W, SUPER_H)
  285.     allocLayers(msgLayer, theLayers, theSuperBitMap, theLayerInfo, theBitMap)
  286.     doLayers(msgLayer, theLayers)
  287.   ENDIF
  288. EXCEPT DO
  289.   disposeLayers(msgLayer, theLayers)
  290.   disposeBitMap(theSuperBitMap, SCREEN_D, SUPER_W, SUPER_H)
  291.   IF msgLayer
  292.     IF FALSE=DeleteLayer(DUMMY, msgLayer)
  293.       error(msgLayer, 'Error deleting layer')
  294.     ENDIF
  295.   ENDIF
  296. ENDPROC
  297. ->>>
  298.  
  299. ->>> PROC runNewView()
  300. -> Set up a low-level graphics display for layers to work on.  Layers should
  301. -> not be built directly on Intuition screens, use a low-level graphics view. 
  302. -> If you need mouse or other events for the layers display, you have to get
  303. -> them directly from the input device.  The only supported method of using
  304. -> layers library calls with Intuition (other than the InstallClipRegion()
  305. -> call) is through Intuition windows.
  306. ->
  307. -> See graphics primitives chapter for details on creating and using the
  308. -> low-level graphics calls.
  309. PROC runNewView() HANDLE
  310.   DEF theView:view, oldview:PTR TO view, theViewPort:viewport,
  311.       theRasInfo, theColourMap=NIL:PTR TO colormap,
  312.       theLayerInfo=NIL:PTR TO layer_info, theBitMap=NIL:PTR TO bitmap,
  313.       colourpalette:PTR TO INT, ktr, gfx:PTR TO gfxbase
  314.  
  315.   -> E-Note: get the right type...
  316.   gfx:=gfxbase
  317.   -> Save current view, to be restored when done
  318.   IF oldview:=gfx.actiview
  319.     -> Get a LayerInfo structure
  320.     theLayerInfo:=NewLayerInfo()
  321.     theColourMap:=GetColorMap(4)
  322.     colourpalette:=theColourMap.colortable;
  323.     FOR ktr:=0 TO 3 DO colourpalette[]++:=colourtable[ktr]
  324.  
  325.     theBitMap:=allocBitMap(SCREEN_D, SCREEN_W, SCREEN_H)
  326.     InitView(theView)
  327.     InitVPort(theViewPort)
  328.  
  329.     theView.viewport:=theViewPort
  330.  
  331.     theRasInfo:=[NIL, theBitMap, 0, 0]:rasinfo
  332.  
  333.     theViewPort.dwidth:=SCREEN_W; theViewPort.dheight:=SCREEN_H
  334.     theViewPort.rasinfo:=theRasInfo; theViewPort.colormap:=theColourMap
  335.  
  336.     MakeVPort(theView, theViewPort); MrgCop(theView); LoadView(theView)
  337.     WaitTOF()
  338.  
  339.     startLayers(theLayerInfo, theBitMap)
  340.  
  341.     -> Put back the old view, wait for it to become active before freeing any
  342.     -> of our display
  343.     LoadView(oldview)
  344.     WaitTOF()
  345.  
  346.     -> Free dynamically created structures
  347.     FreeVPortCopLists(theViewPort)
  348.     FreeCprList(theView.lofcprlist)
  349.   ENDIF
  350. EXCEPT DO
  351.   IF theBitMap THEN disposeBitMap(theBitMap, SCREEN_D, SCREEN_W, SCREEN_H)
  352.   IF theColourMap THEN FreeColorMap(theColourMap)
  353.   IF theLayerInfo THEN DisposeLayerInfo(theLayerInfo)
  354.   ReThrow()
  355. ENDPROC
  356. ->>>
  357.  
  358. ->>> PROC main()
  359. -> Open the libraries used by the example.  Clean up when done.
  360. PROC main() HANDLE
  361.   -> Global constant data for initialising the layers.
  362.   theLayerFlags:=[LAYERSUPER, LAYERSMART, LAYERSIMPLE]:LONG
  363.   colourtable:=[$000, $F44, $4F4, $44F]:INT
  364.  
  365.   layersbase:=OpenLibrary('layers.library', 33)
  366.   runNewView()
  367. EXCEPT DO
  368.   IF layersbase THEN CloseLibrary(layersbase)
  369. ENDPROC
  370. ->>>
  371.  
  372.